<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <!-- 品牌 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: !filterList.brandId }"
          @click="filterList.brandId = ''"
          >全部</a
        >
        <a
          href="javascript:;"
          v-for="i in filterList.brands"
          :key="i.id"
          :class="{ active: filterList.brandId === i.id }"
          @click="filterList.brandId = i.id"
          >{{ i.name }}</a
        >
      </div>
    </div>
    <!-- 属性 -->
    <div class="item" v-for="item in filterList.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: !item.propertiesId }"
          @click="item.propertiesId = ''"
          >全部</a
        >
        <a
          href="javascript:;"
          v-for="i in item.properties"
          :key="i.id"
          :class="{ active: item.propertiesId === i.id }"
          @click="item.propertiesId = i.id"
          >{{ i.name }}</a
        >
      </div>
    </div>
  </div>
</template>
<script>
// import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { watch, ref } from 'vue'
import { categoryFilter } from '../../../api/category'

export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    // const store = useStore()
    const filterList = ref({})
    const route = useRoute()

    watch(
      () => route.params.id,
      newValue => {
        if (newValue && `/category/sub/${newValue}` === route.path) {
          categoryFilter(route.params.id).then(res => {
            // console.log(res)
            filterList.value = res.result
            // console.log(res.result)
          })
        }
      },
      { immediate: true }
    )
    watch(
      () => filterList.value,
      newValue => {
        const temp = {
          brandId: newValue.brandId,
          attrs: newValue.saleProperties.map(item => {
            const findTemp = item.properties.find(sub => {
              return sub.id === item.propertiesId
            })
            // console.log(findTemp)
            return {
              groupName: item.name,
              propertyName: findTemp ? findTemp.name : ''
            }
          })
        }
        // console.log(newValue)
        emit('change', temp)
      },
      { deep: true }
    )
    return { filterList }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
